package main

import (
	"github.com/go-gomail/gomail"
	"io/ioutil"
	"math/rand"
	"net/http"
	"net/http/cookiejar"
	"regexp"
	"strings"
	"time"

	//	"studygolang/email"
	"fmt"
	//      "math/rand"
	//    "time"
)

const requestUrl = "https://studygolang.com/mission/daily/redeem"

var regSuccess = regexp.MustCompile(`<strong>恭喜！</strong> 已成功领取每日登录奖励`)
var regDetail = regexp.MustCompile(`<div class="cell">已连续登录领取 [0-9]* 天，总登录领取 [0-9]* 天</div>`)

func main() {
	rand.Seed(time.Now().UnixNano())
	randNum := rand.Intn(60)
	time.Sleep(time.Minute * time.Duration(randNum))

	client := &http.Client{}
	req, _ := http.NewRequest("GET", requestUrl, nil)

	jar, _ := cookiejar.New(nil)
	jar.SetCookies(req.URL, []*http.Cookie{
		&http.Cookie{Name: "user", Value: "MTU0ODAzOTE1NHxEdi1CQkFFQ180SUFBUkFCRUFBQVVQLUNBQUlHYzNSeWFXNW5EQW9BQ0hWelpYSnVZVzFsQm5OMGNtbHVad3dPQUF4bVlXNWphR1Z1WjJkaGJtY0djM1J5YVc1bkRBc0FDVWxPUkVWWVgxUkJRZ1p6ZEhKcGJtY01CUUFEWVd4c3yRlmvEm2vjzyoJAyvjYg3JqRRmaumQVf2-Pl48nGHvRA==", HttpOnly: true},
		&http.Cookie{Name: "security", Value: "impossible", HttpOnly: true},
	})
	client.Jar = jar

	resp, err := client.Do(req)
	if err != nil {
		panic(err.Error())
	}
	b, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	content := string(b)

	matchSuccessStr := regSuccess.FindStringSubmatch(content)

	if matchSuccessStr == nil && strings.Index(content, "登录") != -1 {
		SendEmailError("登录过期!")
		return
	}

	successStr := matchSuccessStr[0]

	matchSuccessDetailStr := regDetail.FindStringSubmatch(content)

	detailStr := matchSuccessDetailStr[0]

	fmt.Println("send email: ", successStr, detailStr)

	SendEmail(successStr, detailStr)

}

const username = "451922429@qq.com"
const password = "ttkhaniihkkvbidh"
const host = "smtp.qq.com"

func SendEmail(successStr, detailStr string) {
	m := gomail.NewMessage()
	m.SetAddressHeader("From", "451922429@qq.com", "Farahani") // 发件人
	m.SetHeader("To", m.FormatAddress("451922429@qq.com", "Farahani"))
	m.SetHeader("Subject", "StudyGolang签到成功")                                                                                  // 主题
	m.SetBody("text/html", fmt.Sprintf("%s</br>%s", successStr, detailStr)+"</br><a href='https://studygolang.com/'>立即访问</a>") // 正文
	d := gomail.NewDialer(host, 465, username, password)                                                                       // 发送邮件服务器、端口、发件人账号、发件人密码
	if err := d.DialAndSend(m); err != nil {
		fmt.Printf("send mail error: %v", err)
	}
}

func SendEmailError(err string) {
	m := gomail.NewMessage()
	m.SetAddressHeader("From", "451922429@qq.com", "Farahani") // 发件人
	m.SetHeader("To", m.FormatAddress("451922429@qq.com", "Farahani"))
	m.SetHeader("Subject", "StudyGolang签到失败")                                                                // 主题
	m.SetBody("text/html", fmt.Sprintf("<b>%s</b>", err)+"</br><a href='https://studygolang.com/'>立即访问</a>") // 正文
	d := gomail.NewDialer(host, 465, username, password)                                                     // 发送邮件服务器、端口、发件人账号、发件人密码
	if err := d.DialAndSend(m); err != nil {
		fmt.Printf("send mail error: %v", err)
	}

}
